﻿/*
 * Created by SharpDevelop.
 * User: Zhangjin
 * Date: 2013/10/12
 * Time: 14:58
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace EVERStart
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		[DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private int _formWidth;
        private int _formHeight;
        private Rectangle _screenArea;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			_screenArea = System.Windows.Forms.Screen.GetBounds(this);
			this.TopMost = true;//= ConfingUtil.GetBool("TopMost", true);
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void MainFormMouseMove(object sender, MouseEventArgs e)
		{
//			_formWidth = this.Width;
//            _formHeight = this.Height;
//            int x = this.Location.X;
//            int y = this.Location.Y;
//            if (x <= Constants.DOCK_VISCIDITY || x + this.Width >= _screenArea.Width - Constants.DOCK_VISCIDITY)
//            {
//            	this.Location = new System.Drawing.Point(x > 0 ? _screenArea.Width : 0, y);
//                this.Width = 0;
//            }
//            if (y <= Constants.DOCK_VISCIDITY || y + this.Height >= _screenArea.Height - Constants.DOCK_VISCIDITY)
//            {
//            	this.Location = new System.Drawing.Point(x, y > 0 ? _screenArea.Height : 0);
//                this.Height = 0;
//            }
         }
		
		void MainFormLoad(object sender, EventArgs e)
		{
			 _formWidth = this.Width;
       		 _formHeight = this.Height;
		}
		
		void MainFormMouseDown(object sender, MouseEventArgs e)
		{
			ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
		}
		
		void MainFormMouseEnter(object sender, EventArgs e)
		{
			this.Width = _formWidth;
       		this.Height = _formHeight;
       		if (Location.X + this.Width > _screenArea.Width)
       		{
       			Location = new System.Drawing.Point(_screenArea.Width - this.Width, Location.Y);
       		}
       		if (Location.Y + this.Height > _screenArea.Height)
       		{
       			Location = new System.Drawing.Point(Location.X, _screenArea.Height - this.Height);
       		} 
		}
		
		void MainFormLeave(object sender, EventArgs e)
		{
			
		}
		
		void MainFormMouseLeave(object sender, EventArgs e)
		{
			_formWidth = this.Width;
            _formHeight = this.Height;
            int x = this.Location.X;
            int y = this.Location.Y;
            if (x <= 0 || x + this.Width >= _screenArea.Width)
            {
            	this.Location = new System.Drawing.Point(x > 0 ? _screenArea.Width - 2 : 0, y);
                this.Width = 0;
                return;
            }
            if (y <= 0 || y + this.Height >= _screenArea.Height)
            {
            	this.Location = new System.Drawing.Point(x, y > 0 ? _screenArea.Height - 2 : 0);
                this.Height = 0;
				return;
            }
		}
	}
}
